package org.example.likou150.array;

/*
* https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/?envType=study-plan-v2&envId=top-interview-150
* */
public class StrStr {
    public static void main(String[] args) {

    }

    public int strStr(String haystack, String needle) {
        char[] haystackCharArray = haystack.toCharArray();
        char[] needleCharArray = needle.toCharArray();
        for (int i = 0; i <= haystackCharArray.length-needleCharArray.length; i++) {
            if(haystackCharArray[i]==needleCharArray[0]){
                if(check(haystackCharArray, needleCharArray, i)){
                    return i;
                }
            }
        }
        return -1;
    }
    private boolean check(char[] haystackCharArray, char[] needleCharArray, int index){
        for (int i = 0; i < needleCharArray.length; i++) {
            if(haystackCharArray[i+index]!=needleCharArray[i]){
                return false;
            }
        }
        return true;
    }
}
